<template>
  <div class="Login_container">
    <div class="Login_Box">
      <div class="avater_box">
        <img src="../assets/logo.png" />
      </div>
      <!--登录表单区-->
      <el-form ref="LoginFromRef" class="Login_Form" :model="ruleform" :rules="fromrules">
        <el-form-item prop="UserName">
          <el-input v-model="ruleform.UserName" prefix-icon="el-icon-user-solid" placeholder="请输入账号">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleform.Password" prefix-icon="el-icon-lock" placeholder="请输入密码">
          </el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" v-on:click="Login()">登录</el-button>
          <el-button type="info" v-on:click="reseLoginFrom()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import axios from 'axios'
  import qs from 'qs'
  export default {
    data() {
      return {
        ruleform: {
          UserName: '',
          Password: '',
        },
        fromrules: {
          UserName: [{
              required: true,
              message: '请输入账号',
              trigger: 'blur'
            },
            {
              min: 4,
              max: 20,
              message: '长度在 4 到 20 个字符',
              trigger: 'blur'
            }
          ],
          Password: [{
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 6,
              max: 20,
              message: '长度在 6 到 20 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      reseLoginFrom() {
        this.$refs.LoginFromRef.resetFields();
        this.ruleform.UserName = '';
        this.ruleform.Password = '';
      },
      Login() {
        this.$refs.LoginFromRef.validate((async valid => {
          if (!valid) {
            return this.$message.error("用户名或密码错误")
          };

         const {data:res}=await this.$http.post("https://localhost:44389/api/UserLogin",this.ruleform);
        if(res.status !== 200) return this.$message.error(res.message)
        this.$message.success(res.message)
        window.sessionStorage.setItem('token',"Bearer"+" "+res.data);
        this.$router.push("/home");

        }))
      }
    }
  }
</script>

<style lang="less" scoped>
  .Login_container {
    background-color: #2b4b6b;
    height: 100%;
  }

  .Login_Box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avater_box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }

  .Login_Form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }

  .btn {
    display: flex;
    justify-content: flex-end;
  }
</style>
